//
//  SAPNewsCollectionViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/2.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPNewsCollectionViewCell.h"
#import "SAPNewsTwoModel.h"
@implementation SAPNewsCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self header];
        [self footer];
        [self.tableView headerBeginRefreshing];
    }return self;
}

- (void)header {
    _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [self.tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SAPNetWorkTool getWithUrl:news parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                self.oneModel = [[SAPUseCarOneModel alloc] initWithDataSource:dic];
                _HUD.hidden = YES;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];
        });
    }];
}
- (void)footer {
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *str = [[NSString alloc] init];
            for (NSDictionary *dic in self.oneModel.newslistArray) {
                str = [dic valueForKey:@"lasttime"];
            }
            NSString *string = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/news/newslist-pm1-c0-nt1-p1-s30-l%@.json", str];
            [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                SAPUseCarOneModel *oneModel = [[SAPUseCarOneModel alloc] initWithDataSource:dic];
                [self.oneModel.newslistArray addObjectsFromArray:oneModel.newslistArray];
                [self.tableView reloadData];
                [self.tableView footerEndRefreshing];
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
        });
        
    }];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    SAPNewsTwoModel *twoModel = self.oneModel.newslistArray[indexPath.row];
    
    NSString *str = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=1&ourl=http://cont.app.autohome.com.cn/autov5.0.5/content/News/newssharedcontent-n%@.html", twoModel.id];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 100)];
    webView.backgroundColor = [UIColor redColor];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    
    [self.delegate passBaseValue:str];
    
}
@end
